// DBDeployer - The MySQL Sandbox
// Copyright © 2006-2019 Giuseppe Maxia
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package sandbox

var ndbStartTemplate string = `#!{{.ShellPath}}
{{.Copyright}}
# Generated by dbdeployer {{.AppVersion}} using {{.TemplateName}} on {{.DateTime}}
SBDIR={{.SandboxDir}}
BASEDIR={{.Basedir}}
cd $SBDIR

if [ ! -d ndb_conf ]
then
    mkdir ndb_conf
fi

if [ ! -d ndb_conf ]
then
    echo "Directory 'ndb_conf' not found in $PWD"
    exit 1
fi

if [ -f config.ini ]
then
    mv config.ini ndb_conf
fi

if [ -f cluster_initialized ]
then
    echo "cluster already initialized on $(cat cluster_initialized)"
    exit 0
fi

function check_exit_code {
    exit_code=$?
    msg="$1"
    if [ "$exit_code" != "0" ]
    then
        echo "ERROR : $msg"
        exit $exit_code
    fi
}

NUM_NODES={{.NumNodes}}
NUM_NDB_NODES={{.NumNdbNodes}}
cluster_port={{.ClusterPort}}

binary_config=ndb_1_config.bin
count=1

while [ -f ndb_conf/${binary_config}.$count ]
do
    rm -f ndb_conf/${binary_config}.$count
    count=$((count+1))
done

$BASEDIR/bin/ndb_mgmd -f ndb_conf/config.ini \
    --configdir=$PWD/ndb_conf --ndb-connectstring="host=localhost:$cluster_port"
check_exit_code "ndb initialization"

if [ -f stopped_cluster ]
then
    rm -f stopped_cluster
fi

for i in $(seq 2 $NUM_NDB_NODES)
do
    $BASEDIR/bin/ndbmtd --ndb-nodeid=$i --ndb-connectstring="host=localhost:$cluster_port"
    check_exit_code "ndb node $i start"
done

for i in $(seq 1 $NUM_NODES)
do
	echo "executing 'start' on {{.NodeLabel}} $i"
	$SBDIR/{{.NodeLabel}}$i/start 
    check_exit_code "mysql node $i start"
	$SBDIR/{{.NodeLabel}}$i/load_grants
    check_exit_code "mysql node $i load grants"
done
date > cluster_initialized
`

var ndbConfigTemplate string = `
[system]
Name={{.ClusterName}}

{{range .NdbNodes}}
[ndbd]
DataDir={{.SandboxDir}}/ndb{{.NodeLabel}}{{.Node}}
HostName=localhost
NodeId={{.Node}}
{{end}}

[ndb_mgmd]
DataDir={{.SandboxDir}}/ndb{{.NodeLabel}}1
NodeId=1
HostName=localhost
PortNumber={{.ClusterPort}}

{{range .SqlNodes}}
[mysqld]
NodeId={{.Node}}
HostName=localhost
{{end}}

[api]
NodeId={{.LastNode}}
HostName=localhost
`

var ndbStopTemplate string = `#!{{.ShellPath}}
{{.Copyright}}
# Generated by dbdeployer {{.AppVersion}} using {{.TemplateName}} on {{.DateTime}}

SBDIR={{.SandboxDir}}
BASEDIR={{.Basedir}}
CLIENT_BASEDIR={{.ClientBasedir}}

cd $SBDIR

if [ -f cluster_initialized ]
then
    if [ ! -f stopped_cluster ]
    then
        $CLIENT_BASEDIR/bin/ndb_mgm \
            --ndb-connectstring="host=localhost:{{.ClusterPort}}" -e shutdown
    fi
    touch stopped_cluster
    rm -f cluster_initialized
fi

for i in {{.StopNodeList}}
do
	echo "executing 'stop' on {{.NodeLabel}} $i"
	$SBDIR/{{.NodeLabel}}$i/stop 
done
`

var ndbMgmTemplate string = `#!{{.ShellPath}}
{{.Copyright}}
# Generated by dbdeployer {{.AppVersion}} using {{.TemplateName}} on {{.DateTime}}
SBDIR={{.SandboxDir}}
BASEDIR={{.Basedir}}
CLIENT_BASEDIR={{.ClientBasedir}}

$CLIENT_BASEDIR/bin/ndb_mgm \
    --ndb-connectstring="host=localhost:{{.ClusterPort}}" $@
`

var ndbCheckStatusTemplate string = `#!{{.ShellPath}}
{{.Copyright}}
# Generated by dbdeployer {{.AppVersion}} using {{.TemplateName}} on {{.DateTime}}
SBDIR={{.SandboxDir}}

$SBDIR/n1 -e 'select * from ndbinfo.config_nodes cn left join ndbinfo.nodes n using (node_id);'
$SBDIR/ndb_mgm -e show
`

var NdbTemplates = TemplateCollection{
	"ndb_start_cluster": TemplateDesc{
		Description: "NDB start cluster",
		Notes:       "",
		Contents:    ndbStartTemplate,
	},
	"ndb_stop_cluster": TemplateDesc{
		Description: "NDB stop cluster",
		Notes:       "",
		Contents:    ndbStopTemplate,
	},
	"ndb_config_template": TemplateDesc{
		Description: "NDB cluster configuration",
		Notes:       "",
		Contents:    ndbConfigTemplate,
	},
	"ndb_mgm_template": TemplateDesc{
		Description: "NDB cluster manager",
		Notes:       "",
		Contents:    ndbMgmTemplate,
	},
	"ndb_check_status": TemplateDesc{
		Description: "NDB check cluster status",
		Notes:       "",
		Contents:    ndbCheckStatusTemplate,
	},
}
